export class Hero {
  constructor(
    public name: string,
    public maxHealth: number,
    public attack: number,
    public defense: number,
    public level: number,
    public currentHealth: number = maxHealth,
    public experience: number = 0,
    public experienceToNextLevel: number = 100
  ) {}

  // 显示状态
  displayStatus(): void {
    console.log(`等级: ${this.level}`);
    console.log(`生命值: ${this.currentHealth}/${this.maxHealth}`);
    console.log(`攻击力: ${this.attack}`);
    console.log(`防御力: ${this.defense}`);
    console.log(`经验值: ${this.experience}/${this.experienceToNextLevel}`);
  }

  // 攻击方法
  attackEnemy(enemy: any): void {
    const damage = Math.max(1, this.attack - enemy.defense);
    enemy.currentHealth -= damage;
    console.log(`${this.name} 攻击了 ${enemy.name}，造成 ${damage} 点伤害！`);
  }

  // 受到伤害
  takeDamage(damage: number): void {
    const actualDamage = Math.max(1, damage - this.defense);
    this.currentHealth -= actualDamage;
    if (this.currentHealth < 0) this.currentHealth = 0;
    console.log(`${this.name} 受到了 ${actualDamage} 点伤害！`);
  }

  // 获得经验值
  gainExperience(amount: number): void {
    this.experience += amount;
    console.log(`${this.name} 获得了 ${amount} 点经验值！`);
    
    // 检查是否升级
    this.checkLevelUp();
  }

  // 检查升级
  private checkLevelUp(): void {
    while (this.experience >= this.experienceToNextLevel) {
      this.levelUp();
    }
  }

  // 升级
  private levelUp(): void {
    this.level++;
    this.experience -= this.experienceToNextLevel;
    this.experienceToNextLevel = Math.floor(this.experienceToNextLevel * 1.5);
    
    // 升级属性
    this.maxHealth += 20;
    this.attack += 5;
    this.defense += 3;
    this.currentHealth = this.maxHealth;
    
    console.log(`🎉 ${this.name} 升级了！现在是 ${this.level} 级！`);
    console.log(`属性提升：生命值 +20, 攻击力 +5, 防御力 +3`);
  }

  // 恢复生命值
  restoreHealth(amount: number): void {
    this.currentHealth = Math.min(this.maxHealth, this.currentHealth + amount);
    console.log(`${this.name} 恢复了 ${amount} 点生命值！`);
  }

  // 判断是否存活
  isAlive(): boolean {
    return this.currentHealth > 0;
  }
}